//
//  ResourceViewController.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/3.
//  Copyright © 2019 李文强. All rights reserved.
//

#import "ResourceViewController.h"
#import "SYFunctionView.h"
#import "SYFunctoinModel.h"
#import "SYCollectionViewController.h"//收藏
#import "SYClassPlayerViewController.h"//上课视频
#import "SYShangBaoViewController.h"//资源下发

@interface ResourceViewController ()
@property (nonatomic, strong) NSMutableArray *dataArray;
//所有的小窗的导航
@property (nonatomic, strong) NSMutableArray *allMiniNavArray;
@property (nonatomic, strong) NSMutableArray *allViewArray;
@property (nonatomic, assign) int minuType;
@property (strong, nonatomic) SYFunctionView *functionView;

@property (strong, nonatomic) NSMutableArray *numberArr;
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) UIView *contView;



@property (strong, nonatomic) SYCollectionViewController *collectionVC;//收藏
@property (strong, nonatomic) SYCollectionViewController *wenJianCollectionVC;//我的作品
@property (strong, nonatomic) SYCollectionViewController *jiayanCollectionVC; //教研文件
@property (strong, nonatomic) SYCollectionViewController *jigouCollectionVC;//机构
@property (strong, nonatomic) SYCollectionViewController *weikeCollectionVC;//微课
@property (strong, nonatomic) SYClassPlayerViewController *shiPinCollectionVC;//微课
@property (strong, nonatomic) SYShangBaoViewController *shangbaoVC;//资源上报


@end

@implementation ResourceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUpUIKit];
    
}

- (void)setUpUIKit {
    __weak typeof(self) weakSelf = self;
    if (self.isChar) {
        self.numberArr = [[SYFunctoinModel alloc] chatDataArr];
    } else if (self.IsFrom.length > 0) {
        self.numberArr = [[SYFunctoinModel alloc] fromDataArr];
    } else {
        self.numberArr = [[SYFunctoinModel alloc] monishuju];
    }
    
    _functionView = [[SYFunctionView alloc] initWithFrame:CGRectMake(0, 20, 215, APP_HEIGHT - 20)];
    [_functionView showFunctionView:_functionView vc:self listDataArr:self.numberArr];

    _functionView.fenXiangFromBlcok = ^{
        [weakSelf dismissViewControllerAnimated:YES completion:^{
            
        }];
        return ;
    };
    [_functionView setViewFromType:self.IsFrom];
    [self.view addSubview:_functionView];
    [self.view addSubview:self.scrollView];
    _functionView.block = ^(SYFunctoinModel *model, NSInteger index) {
        [weakSelf switchInterfaceIndex:index];
    };
    
    
    self.scrollView.contentSize = CGSizeMake(0, (APP_HEIGHT-20)*self.numberArr.count);
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.functionView.mas_right);
        make.top.equalTo(@20);
        make.right.equalTo(@0);
        make.bottom.equalTo(self.view.mas_bottom);
    }];
    
//    [self.scrollView addSubview:self.contView];
//    [self.contView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.bottom.right.left.equalTo(self.scrollvi);
//    }];
    for (NSInteger i = 0; i <self.numberArr.count; i++) {
        if (self.numberArr.count == 0) {
            return;
        }
        CGFloat height = APP_HEIGHT - 20;
//        CGFloat width = CGRectGetWidth(self.scrollView.frame);
        CGFloat Y = i * height;
        SYFunctoinModel *function = self.numberArr[i];
        if ([function.titleName containsString:@"收藏"]) {
            self.collectionVC = [[SYCollectionViewController alloc] init];
            self.collectionVC.isChar = self.isChar;//从聊天页面进来
            __weak typeof(self) weakSelf = self;
            self.collectionVC.chooseZiYuanRetureBlock = ^(SYCollectionModel *model) {
                if (weakSelf.hasChooseZiYuanMoDel) {
                    weakSelf.hasChooseZiYuanMoDel(model);
                }
                [weakSelf dismissViewControllerAnimated:YES completion:^{
                    
                }];
            };
            self.collectionVC.IsFrom = self.IsFrom;
            [self addChildViewController:self.collectionVC];
            self.collectionVC.functionModel = function;
//            self.collectionVC.functionModel.
//            self.collectionVC.view.frame = CGRectMake(0, Y,width , height);//
            [self.scrollView addSubview:self.collectionVC.view];
            [self.collectionVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.functionView.mas_right);
                make.right.equalTo(self.view.mas_right).offset(0);
                make.top.equalTo(self.scrollView.mas_top).offset(0);
                make.height.mas_equalTo(height);
            }];
        } else if ([function.titleName containsString:@"我的作品"]) {
            self.wenJianCollectionVC = [[SYCollectionViewController alloc] init];
            __weak typeof(self) weakSelf = self;
            self.wenJianCollectionVC.isChar = self.isChar;//从聊天页面进来
            self.wenJianCollectionVC.chooseZiYuanRetureBlock = ^(SYCollectionModel *model) {
                if (weakSelf.hasChooseZiYuanMoDel) {
                    weakSelf.hasChooseZiYuanMoDel(model);
                }
                [weakSelf dismissViewControllerAnimated:YES completion:^{
                    
                }];
            };
            self.wenJianCollectionVC.IsFrom = self.IsFrom;

            [self addChildViewController:self.wenJianCollectionVC];
            self.wenJianCollectionVC.functionModel = function;
//            self.wenJianCollectionVC.view.frame = CGRectMake(0, Y,width , height);
            [self.scrollView addSubview:self.wenJianCollectionVC.view];
            [self.wenJianCollectionVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.functionView.mas_right);
                make.right.equalTo(self.view.mas_right).offset(0);
                make.top.equalTo(self.scrollView.mas_top).offset(Y);
                make.height.mas_equalTo(height);
            }];
        } else if ([function.titleName containsString:@"教研"]) {
            self.jiayanCollectionVC = [[SYCollectionViewController alloc] init];
            self.jiayanCollectionVC.IsFrom = self.IsFrom;
            __weak typeof(self) weakSelf = self;
            self.jiayanCollectionVC.chooseZiYuanRetureBlock = ^(SYCollectionModel *model) {
                if (weakSelf.hasChooseZiYuanMoDel) {
                    weakSelf.hasChooseZiYuanMoDel(model);
                }
                [weakSelf dismissViewControllerAnimated:YES completion:^{
                    
                }];
            };
            [self addChildViewController:self.jiayanCollectionVC];
            self.jiayanCollectionVC.functionModel = function;
//            self.jiayanCollectionVC.view.frame = CGRectMake(0, Y,width , height);
            [self.scrollView addSubview:self.jiayanCollectionVC.view];
            [self.jiayanCollectionVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.functionView.mas_right);
                make.right.equalTo(self.view.mas_right).offset(0);
                make.top.equalTo(self.scrollView.mas_top).offset(Y);
                make.height.mas_equalTo(height);
            }];
        } else if ([function.titleName containsString:@"机构"]) {
            self.jigouCollectionVC = [[SYCollectionViewController alloc] init];
            self.jigouCollectionVC.IsFrom = self.IsFrom;
            __weak typeof(self) weakSelf = self;
            self.jigouCollectionVC.chooseZiYuanRetureBlock = ^(SYCollectionModel *model) {
                if (weakSelf.hasChooseZiYuanMoDel) {
                    weakSelf.hasChooseZiYuanMoDel(model);
                }
                [weakSelf dismissViewControllerAnimated:YES completion:^{
                    
                }];
            };
            [self addChildViewController:self.jigouCollectionVC];
            self.jigouCollectionVC.functionModel = function;
//            self.jigouCollectionVC.view.frame = CGRectMake(0, Y,width , height);
            [self.scrollView addSubview:self.jigouCollectionVC.view];
            [self.jigouCollectionVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.functionView.mas_right);
                make.right.equalTo(self.view.mas_right).offset(0);
                make.top.equalTo(self.scrollView.mas_top).offset(Y);
                make.height.mas_equalTo(height);
            }];
        } else if ([function.titleName containsString:@"微课"]) {
            self.weikeCollectionVC = [[SYCollectionViewController alloc] init];
            self.weikeCollectionVC.IsFrom = self.IsFrom;
            __weak typeof(self) weakSelf = self;
            self.weikeCollectionVC.chooseZiYuanRetureBlock = ^(SYCollectionModel *model) {
                if (weakSelf.hasChooseZiYuanMoDel) {
                    weakSelf.hasChooseZiYuanMoDel(model);
                }
                [weakSelf dismissViewControllerAnimated:YES completion:^{
                    
                }];
            };
            [self addChildViewController:self.weikeCollectionVC];
            self.weikeCollectionVC.functionModel = function;
//            self.weikeCollectionVC.view.frame = CGRectMake(0, Y,width , height);
            [self.scrollView addSubview:self.weikeCollectionVC.view];
            [self.weikeCollectionVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.functionView.mas_right);
                make.right.equalTo(self.view.mas_right).offset(0);
                make.top.equalTo(self.scrollView.mas_top).offset(Y);
                make.height.mas_equalTo(height);
            }];
        } else if ([function.titleName containsString:@"视频"]) {
            self.shiPinCollectionVC = [[SYClassPlayerViewController alloc] init];

            self.shiPinCollectionVC.type = SYClassPlayerTutorTypeUp;
            [self addChildViewController:self.shiPinCollectionVC];
//            self.shiPinCollectionVC.functionModel = function;
            
            [self.scrollView addSubview:self.shiPinCollectionVC.view];
            [self.shiPinCollectionVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.functionView.mas_right);
                make.right.equalTo(self.view.mas_right).offset(0);
                make.top.equalTo(self.scrollView.mas_top).offset(Y);
                make.height.mas_equalTo(height);
            }];
        } else if ([function.titleName containsString:@"资源上报"]) {
            self.shangbaoVC = [[SYShangBaoViewController alloc] init];
            [self addChildViewController:self.shangbaoVC];
            [self.scrollView addSubview:self.shangbaoVC.view];
            [self.shangbaoVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.functionView.mas_right);
                make.right.equalTo(self.view.mas_right).offset(0);
                make.top.equalTo(self.scrollView.mas_top).offset(Y);
                make.height.mas_equalTo(height);
            }];
        }
    }
    
    [(SYCollectionViewController *) self.childViewControllers[0] viewDidSlidInto];
    
}
//切换界面
- (void)switchInterfaceIndex:(NSInteger)index {
    CGFloat X = 0;
    CGFloat Y = index * (APP_HEIGHT - 20);
    CGPoint offset = CGPointMake(X, Y);
    [self.scrollView setContentOffset:offset animated:NO];
    if (index == 3) {
        [(SYClassPlayerViewController *) self.childViewControllers[index] viewDidSlidInto];
    } else if (index == 4) {//资源上报
        [(SYShangBaoViewController *) self.childViewControllers[index] viewDidSlidInto];
    }else {
        [(SYCollectionViewController *) self.childViewControllers[index] viewDidSlidInto];
    }
    
}


#pragma mark - setget
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.scrollEnabled = NO;
    }
    return _scrollView;
}

- (UIView *)contView {
    if (!_contView) {
        _contView = [[UIView alloc] init];
    }
    return _contView;
}

@end
